package com.xinshujia.wxxcx.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.xinshujia.wxxcx.mapper.AlarmRecordMapper;

import com.xinshujia.wxxcx.mapper.AlarmeventlogMapper;
import com.xinshujia.wxxcx.mapper.MachineMapper;
import com.xinshujia.wxxcx.mapper.ProductionLineMapper;
import com.xinshujia.wxxcx.pojos.entity.AlarmRecord;
import com.xinshujia.wxxcx.pojos.entity.Alarmeventlog;
import com.xinshujia.wxxcx.pojos.entity.ProductionLine;
import com.xinshujia.wxxcx.pojos.info.AlarmRecordCause;
import com.xinshujia.wxxcx.pojos.info.AlarmRecordInfo;
import com.xinshujia.wxxcx.pojos.info.AlarmRecordNumInfo;
import com.xinshujia.wxxcx.pojos.info.WxUserInfo;
import com.xinshujia.wxxcx.pojos.view.AlarmRecordView;
import com.xinshujia.wxxcx.pojos.vo.MachineVos;
import com.xinshujia.wxxcx.service.AlarmRecordService;
import com.xinshujia.wxxcx.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Cui Han
 * @Date 2024/5/17 15:39
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class AlarmRecordServiceImpl implements AlarmRecordService {

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    @Autowired
    private AlarmeventlogMapper alarmeventlogMapper;

    @Autowired
    private MachineMapper machineMapper;

    @Autowired
    private ProductionLineMapper productionLineMapper;

    /**
     * 查询报警列表
     *
     * @return
     */
    @Override
    public List<AlarmRecordView> findAlarmRecord() {
        WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
        String company = wxUserInfo.getCompany();
        return  alarmRecordMapper.findAlarmRecord(company);
    }

    /**
     * 报警详情
     *
     * @param id
     * @return
     */
    @Override
    public AlarmRecordCause findByIdAlarmCause(Integer id) {
        return alarmRecordMapper.selectAlarmCause(id);
    }

    /**
     * 修改报警详情
     *
     * @param alarmRecordCause
     * @return
     */
    @Override
    public Result updateAlarmCause(AlarmRecordCause alarmRecordCause) {
        AlarmRecord alarmRecord = alarmRecordMapper.selectById(alarmRecordCause.getId());
        String photos = alarmRecordCause.getPhoto();
        if (photos == null) {
            return Result.build(null, 400, "图片不能为空");
        }
        alarmRecord.setPhoto(photos);
        String cause = alarmRecordCause.getCause();
        if (cause == null){
            return Result.build(null,400,"原因不能为空");
        }
        alarmRecord.setCause(cause);
        WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
        String user = wxUserInfo.getName();
        alarmRecord.setUser(user);
        alarmRecord.setDispose(1);
        alarmRecordMapper.updateAlarmCause(alarmRecord);
        return Result.ok("提交成功");
    }

    /**
     * 查询机床报警排名
     *
     * @return
     */
    @Override
    public List<AlarmRecordNumInfo> findAlarmRecordNum() {
        WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
        String company = wxUserInfo.getCompany();
        return alarmRecordMapper.findAlarmRecordNum(company);
    }

    /**
     * 新增报警记录
     */
    @Override
    public void insertAlarmRecord() {
        Date date = new Date();
        date.setTime(date.getTime() /1000*1000);
        List<Alarmeventlog> alarmeventlogs = alarmeventlogMapper.selectByTime(date);
        if (alarmeventlogs!=null && !alarmeventlogs.isEmpty()){
            for (Alarmeventlog alarmeventlog : alarmeventlogs) {
                String device = alarmeventlog.getDeviceGUID();
                MachineVos machineVos = machineMapper.selectMachine(device);
                Long machineId = machineVos.getId();
                Long productionLineId = machineVos.getProductionLineId();
                ProductionLine productionLine = productionLineMapper.selectCompany(productionLineId);
                String company = productionLine.getCompany();
                AlarmRecord alarmRecord = new AlarmRecord();
                alarmRecord.setDate(date);
                alarmRecord.setProductionLineId(productionLineId);
                alarmRecord.setMachineId(machineId);
                alarmRecord.setCompany(company);
                alarmRecord.setDevice(device);
                alarmRecord.setDispose(0);
                alarmRecord.setStatus(3);
                alarmRecordMapper.insertAlarmRecord(alarmRecord);
            }
        }
    }

    /**
     * 报警弹窗
     *
     * @return
     */
    @Override
    public Result findAlarm() {
        WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
        String company = wxUserInfo.getCompany();
        AlarmRecordInfo alarmRecordInfo = alarmRecordMapper.selectAlarm(company);
        if (alarmRecordInfo == null){
            return Result.ok("无报警");
        }
        return Result.ok(alarmRecordInfo);
    }
}
